package com.war.controller.outofstorage;

import com.war.dao.outOfStorage.ProductOutDao;
import com.war.dao.stock.DamagesDao;
import com.war.pojo.outofstorage.ProductOut;
import com.war.pojo.stock.DamagesInfo;
import com.war.pojo.stock.TransferInfo;
import com.war.pojo.system.*;
import com.war.pojo.warehousing.PurchaseInfo;
import com.war.service.outofstorage.ProductOutService;
import com.war.service.safeManage.UserService;
import com.war.service.system.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.text.html.HTML;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/outOfStorage")
public class ProductOutController {
    @Autowired(required = false)
    ProductOutService productOutService;
    //访问productOut-list页面
    @RequestMapping("/out")
    public String list( ProductOut po,ModelMap m){
        HashMap<String,Object> map= productOutService.select(po);
        m.put("info",map);

        m.put("preCondition",po.getCondition());
        m.put("start",po.getStartDate());
        m.put("end",po.getEndDate());
        return "outOfStorage/productOut-list";
    }
    //录入
    @RequestMapping("/add")
    public String add(){
        return "outOfStorage/productOut-add";
    }

    @RequestMapping("/addAjax")
    @ResponseBody
    public HashMap<String,Object> addAjax(ProductOut po){
        HashMap<String,Object> map = new HashMap<String,Object>();

        String info=productOutService.add(po);
        map.put("info",info);
        return map;
    }
    //打开详情页面
    @RequestMapping("/getDetail")
    public String getDetail(ProductOut po,ModelMap m){
        ProductOut info=productOutService.selectByOutId(po);
        m.addAttribute("info",info);
        return "outOfStorage/productOut-detail";
    }

    //处理比较入库数量与仓库所能容纳的数量的ajax请求
    @RequestMapping("/storageNumNow")
    @ResponseBody
    public HashMap<String,Object> storageNumNow(ProductOut po){
        HashMap<String,Object> map = new HashMap<>();
        int result = productOutService.storageNumNow(po);
        map.put("info",result);
        return map;
    }
    //加载客户名字
    @RequestMapping("/clientName")
    @ResponseBody
    public List<Customer> selectClientName(){
        return productOutService.selectClientName();
    }

    //加载客户电话
    @RequestMapping("/getClientTel")
    @ResponseBody
    public String getClientTel(Customer customer){
        return productOutService.getClientTel(customer);
    }

    //加载仓库名称
    @RequestMapping("/warehouseName")
    @ResponseBody
    public List<Warehouse> selectWarehouseName(){
        return productOutService.selectWarehouseName();
    }
    //获取仓库的管理员名称
    @RequestMapping("/getAdministrator")
    @ResponseBody
    public String getAdministrator(Warehouse warehouse){
        return productOutService.getAdministrator(warehouse);
    }

    //处理删除的ajax
    @RequestMapping("/delInfo")
    @ResponseBody
    public HashMap<String,Object> delInfo(ProductOut po){
        HashMap<String,Object> map=new HashMap<>();
        String result=productOutService.delInfo(po);
        map.put("info",result);
        return map;
    }

    //打开修改页面
    @RequestMapping("/update")
    public String update(ProductOut po,ModelMap m){
        ProductOut info=productOutService.selectByOutId(po);
        m.addAttribute("info",info);
        return "outOfStorage/productOut-edit";
    }
    //处理修改的ajax
    @RequestMapping("/updateAjax")
    @ResponseBody
    public HashMap<String,Object> editAjax(ProductOut po){
        HashMap<String,Object> map=new HashMap<>();
        String result=productOutService.edit(po);
        map.put("info",result);
        return map;
    }

    //批量删除
    @RequestMapping(value = "delAll",method = RequestMethod.POST)
    @ResponseBody
    public HashMap<String,Object> delAll(@RequestBody List<ProductOut> po){
        HashMap<String,Object> map=new HashMap<String, Object>();
        String result=productOutService.delAll(po);
        map.put("info",result);
        return map;
    }

    //退货表单页面
    @RequestMapping("/productReturn")
    public String productReturn(ProductOut po, ModelMap m){
        //获得对应的采购记录
        ProductOut reInfo = productOutService.selectByOutId(po);
        //把数据传到前端
        m.addAttribute("reInfo",reInfo);
        return "outOfStorage/productOut-return";
    }

    //处理退货的ajax请求
    @RequestMapping("/productReturnAjax")
    @ResponseBody
    public HashMap<String,Object> productReturn( ){
        HashMap<String,Object> map = new HashMap<>();
        String result = productOutService.productReturn();
        map.put("info",result);
        return map;
    }


}
